import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Welcome from '../views/Welcome.vue'

import UserList from '../views/user/UserList.vue'
import LogList from '../views/log/LogList.vue'
import FileList from '../views/file/FileList.vue'
import GoodsList from '../views/goods/GoodsList.vue'
import PayList from '../views/pay/PayList.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: "/login",
    component: Login
  },
  {
    path: "/home",
    component: Home,
    redirect: "/welcome",
    children: [
      { path: "/welcome", component: Welcome, },
      { path: "/user/list", component: UserList, },
      { path: "/log/list", component: LogList, },
      { path: "/file/list", component: FileList, },
      { path: "/goods/list", component: GoodsList, },
      { path: "/pay/list", component: PayList, }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to:将要访问的路径
  // from:从哪里访问的路径
  // next:之后要做的任务，是一个函数
  //    next（）放行， next（'/URL'）强制跳转的路径。
  if (to.path == '/login') return next();// 访问路径为登录
  // 获取token
  const tokenStr = window.sessionStorage.getItem("Token");// session取值
  if (!tokenStr) return next('/login');// 没登录去登录
  next();
})

export default router
